<%
Class FastAsp_Page
    Private m_TotalRows    '总记录数
    Private m_PageSize     '每页记录数
    Private m_CurrentPage  '当前页码
    Private m_TotalPages   '总页数
    Private m_UrlRule      'URL规则
    Private m_Style        '分页样式
    Private m_PageParam    '分页参数名
    Private m_ShowTotal    '是否显示总数
    Private m_NumCount     '显示的页码数量
    
    Private Sub Class_Initialize()
        m_TotalRows = 0
        m_PageSize = 10
        m_CurrentPage = 1
        m_TotalPages = 0
        m_UrlRule = ""
        m_Style = GetConfig("PageStyle")
        m_PageParam = GetConfig("PageParam")
        m_ShowTotal = GetConfig("PageShowTotal")
        m_NumCount = GetConfig("PageNumCount")
    End Sub
    
    '设置总记录数
    Public Property Let TotalRows(byval value)
        m_TotalRows = value
        '计算总页数
        If m_PageSize > 0 Then
            m_TotalPages = Int((m_TotalRows + m_PageSize - 1) / m_PageSize)
        End If
    End Property
    
    '获取总记录数
    Public Property Get TotalRows
        TotalRows = m_TotalRows
    End Property
    
    '设置每页记录数
    Public Property Let PageSize(byval value)
        m_PageSize = value
        '重新计算总页数
        If m_PageSize > 0 Then
            m_TotalPages = Int((m_TotalRows + m_PageSize - 1) / m_PageSize)
        End If
    End Property
    
    '获取每页记录数
    Public Property Get PageSize
        PageSize = m_PageSize
    End Property
    
    '设置当前页码
    Public Property Let CurrentPage(byval value)
        If IsNumeric(value) Then
            m_CurrentPage = Int(value)
            '页码范围检查
            If m_CurrentPage < 1 Then m_CurrentPage = 1
            If m_CurrentPage > m_TotalPages And m_TotalPages > 0 Then
                m_CurrentPage = m_TotalPages
            End If
        End If
    End Property
    
    '获取当前页码
    Public Property Get CurrentPage
        CurrentPage = m_CurrentPage
    End Property
    
    '获取总页数
    Public Property Get TotalPages
        TotalPages = m_TotalPages
    End Property
    
    '设置URL规则
    Public Property Let UrlRule(byval value)
        m_UrlRule = value
    End Property
    
    '设置分页样式
    Public Property Let Style(byval value)
        m_Style = value
    End Property
    
    '获取分页URL
    Private Function GetPageUrl(byval page)
        Dim url
        If m_UrlRule <> "" Then
            url = Replace(m_UrlRule, "{page}", page)
        Else
            url = ReplaceParam(m_PageParam, page)
        End If
        GetPageUrl = url
    End Function
    
    '替换URL参数
    Private Function ReplaceParam(byval paramName, byval paramValue)
        Dim url, queryString, params, i, param, key, value
        url = Request.ServerVariables("URL")
        queryString = Request.ServerVariables("QUERY_STRING")
        
        If queryString <> "" Then
            params = Split(queryString, "&")
            queryString = ""
            For i = 0 To UBound(params)
                param = Split(params(i), "=")
                key = param(0)
                If UBound(param) > 0 Then
                    value = param(1)
                Else
                    value = ""
                End If
                
                If LCase(key) <> LCase(paramName) Then
                    If queryString <> "" Then queryString = queryString & "&"
                    queryString = queryString & key & "=" & value
                End If
            Next
            
            If queryString <> "" Then
                url = url & "?" & queryString & "&" & paramName & "=" & paramValue
            Else
                url = url & "?" & paramName & "=" & paramValue
            End If
        Else
            url = url & "?" & paramName & "=" & paramValue
        End If
        
        ReplaceParam = url
    End Function
    
    '显示分页
    Public Function Show()
        If m_Style = "bootstrap" Then
            Show = ShowBootstrap()
        ElseIf m_Style = "simple" Then
            Show = ShowSimple()
        Else
            Show = ShowCustom()
        End If
    End Function
    
    'Bootstrap样式分页
    Private Function ShowBootstrap()
        Dim html, i, start, ending
        
        html = "<nav><ul class=""pagination"">"
        
        '首页
        If m_CurrentPage > 1 Then
            html = html & "<li><a href=""" & GetPageUrl(1) & """>" & GetConfig("PageFirstText") & "</a></li>"
        Else
            html = html & "<li class=""disabled""><span>" & GetConfig("PageFirstText") & "</span></li>"
        End If
        
        '上一页
        If m_CurrentPage > 1 Then
            html = html & "<li><a href=""" & GetPageUrl(m_CurrentPage-1) & """>" & GetConfig("PagePrevText") & "</a></li>"
        Else
            html = html & "<li class=""disabled""><span>" & GetConfig("PagePrevText") & "</span></li>"
        End If
        
        '页码
        start = m_CurrentPage - Int(m_NumCount/2)
        If start < 1 Then start = 1
        ending = start + m_NumCount - 1
        If ending > m_TotalPages Then
            ending = m_TotalPages
            start = ending - m_NumCount + 1
            If start < 1 Then start = 1
        End If
        
        For i = start To ending
            If i = m_CurrentPage Then
                html = html & "<li class=""active""><span>" & i & "</span></li>"
            Else
                html = html & "<li><a href=""" & GetPageUrl(i) & """>" & i & "</a></li>"
            End If
        Next
        
        '下一页
        If m_CurrentPage < m_TotalPages Then
            html = html & "<li><a href=""" & GetPageUrl(m_CurrentPage+1) & """>" & GetConfig("PageNextText") & "</a></li>"
        Else
            html = html & "<li class=""disabled""><span>" & GetConfig("PageNextText") & "</span></li>"
        End If
        
        '末页
        If m_CurrentPage < m_TotalPages Then
            html = html & "<li><a href=""" & GetPageUrl(m_TotalPages) & """>" & GetConfig("PageLastText") & "</a></li>"
        Else
            html = html & "<li class=""disabled""><span>" & GetConfig("PageLastText") & "</span></li>"
        End If
        
        '显示总数
        If m_ShowTotal Then
            html = html & "<li class=""disabled""><span>共" & m_TotalRows & "条/" & m_TotalPages & "页</span></li>"
        End If
        
        html = html & "</ul></nav>"
        ShowBootstrap = html
    End Function
    
    '简单样式分页
    Private Function ShowSimple()
        Dim html
        
        html = "<div class=""pagination"">"
        
        '首页
        If m_CurrentPage > 1 Then
            html = html & "<a href=""" & GetPageUrl(1) & """>" & GetConfig("PageFirstText") & "</a>"
        Else
            html = html & "<span class=""disabled"">" & GetConfig("PageFirstText") & "</span>"
        End If
        
        '上一页
        If m_CurrentPage > 1 Then
            html = html & "<a href=""" & GetPageUrl(m_CurrentPage-1) & """>" & GetConfig("PagePrevText") & "</a>"
        Else
            html = html & "<span class=""disabled"">" & GetConfig("PagePrevText") & "</span>"
        End If
        
        '当前页/总页数
        html = html & "<span class=""current"">" & m_CurrentPage & "/" & m_TotalPages & "</span>"
        
        '下一页
        If m_CurrentPage < m_TotalPages Then
            html = html & "<a href=""" & GetPageUrl(m_CurrentPage+1) & """>" & GetConfig("PageNextText") & "</a>"
        Else
            html = html & "<span class=""disabled"">" & GetConfig("PageNextText") & "</span>"
        End If
        
        '末页
        If m_CurrentPage < m_TotalPages Then
            html = html & "<a href=""" & GetPageUrl(m_TotalPages) & """>" & GetConfig("PageLastText") & "</a>"
        Else
            html = html & "<span class=""disabled"">" & GetConfig("PageLastText") & "</span>"
        End If
        
        '显示总数
        If m_ShowTotal Then
            html = html & "<span class=""total"">共" & m_TotalRows & "条记录</span>"
        End If
        
        html = html & "</div>"
        ShowSimple = html
    End Function
    
    '自定义样式分页
    Private Function ShowCustom()
        '可以在这里实现自定义样式的分页
        ShowCustom = ShowBootstrap()
    End Function
End Class
%> 